package com.example.shopcardemo;

import java.util.Properties;
import java.util.UUID;

import org.apache.http.client.HttpClient;
import org.apache.http.client.params.CookiePolicy;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
 
 
/**
 * 全局应用程序类：用于保存和调用全局应用配置及访问网络数据
 * 
 */
public class AppContext extends BaseApplication {

    public static final int PAGE_SIZE = 20;// 默认分页大小

    private static AppContext instance;

    private String  tel;
    
    private boolean login;

    private String token;//访问秘钥
    
   // private String upToken;
    
    private double pushRadius;
    
//     private String pushLag="1";
    
    public static final String KEY_FRITST_START = "KEY_FRIST_START";
    
    /**
     * 储存购物车数据
     */
    private static Bundle bundle;
    
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
        // Thread.setDefaultUncaughtExceptionHandler(AppException
        // .getAppExceptionHandler(this));
    }

    private void init() {
        // 初始化网络请求
         
        bundle=new Bundle();

/*        // Log控制器
        KJLoger.openDebutLog(true);
        TLog.DEBUG = BuildConfig.DEBUG;
*/
        // Bitmap缓存地址
       // BitmapConfig.CACHEPATH = "Sgone/imagecache";
    }

   
    /**
     * 获得当前app运行的AppContext
     * 
     * @return
     */
    public static AppContext getInstance() {
        return instance;
    }

    public boolean containsProperty(String key) {
        Properties props = getProperties();
        return props.containsKey(key);
    }

    public void setProperties(Properties ps) {
        AppConfig.getAppConfig(this).set(ps);
    }

    public Properties getProperties() {
        return AppConfig.getAppConfig(this).get();
    }

    public void setProperty(String key, String value) {
        AppConfig.getAppConfig(this).set(key, value);
    }
    
    public static Bundle getApplicationBundle(){
    	return bundle;
    }

    /**
     * 获取cookie时传AppConfig.CONF_COOKIE
     * 
     * @param key
     * @return
     */
    public String getProperty(String key) {
        String res = AppConfig.getAppConfig(this).get(key);
        return res;
    }

    public void removeProperty(String... key) {
        AppConfig.getAppConfig(this).remove(key);
    }

    /**
     * 获取App唯一标识
     * 
     * @return
     */
    public String getAppId() {
        String uniqueID = getProperty(AppConfig.CONF_APP_UNIQUEID);
        if (StringUtils.isEmpty(uniqueID)) {
            uniqueID = UUID.randomUUID().toString();
            setProperty(AppConfig.CONF_APP_UNIQUEID, uniqueID);
        }
        return uniqueID;
    }

    /**
     * 获取App安装包信息
     * 
     * @return
     */
    public PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if (info == null)
            info = new PackageInfo();
        return info;
    }
     
    
 
    
/*	*//**
	 * 判断百度推送是否绑定
	 * @return
	 *//*
    public static boolean hasBind() {
    	return get(KEY_BAIDU_BIND,false);
    }

    public static void setBind(final boolean flag) {
    	set(KEY_BAIDU_BIND,flag);
    }
    

    
    /**
     * 更新用户信息
     * @param name  修改用户信息的名称
     * @param value  修改用户名称的值
     */
    @SuppressWarnings("serial")
    public void updateUserInfo(final String name,final String value) {
        setProperties(new Properties() {
            {
            	setProperty(name, value);
            }
        });
    }

     
    /**
     * 清除登录信息
     */
    public void cleanLoginInfo() {
        this.tel = "";
        this.login = false;
        removeProperty("user.sId","user.sTel", "user.shopName", "user.address", "user.pwd",
                "user.province", "user.city", "user.town","user.shopImage","user.shopLicence",
                "user.uToken",  "user.shopUrl","user.push_flag","user.push_radius","user.closeTime",
                "user.openTime","user.startPrice","user.transPrice","user.is_reward",
                "user.has_reward","user.has_charge","user.monetary"); 
    }

    public String getTel() {
        return tel;
    }
    
    public String getToken() {
/*    	if(token==null)
    	{
    		getProperty("user.uToken");
    	}*/
    	return token;
    }
    
    /**
     * 设置关店或开店
     * 
     * 
     */
    public boolean getPushFlag() {
     	return   getProperty("user.push_flag").equals("1")?true:false;
    }
    
    public void setPushFlag(boolean tag)
    {
    	String flag = tag?"1":"0";
    	updateUserInfo("user.push_flag", flag);
    	//setProperty("user.push_flag", flag);
    }
 
    public boolean isLogin() {
        return login;
    }
    
    public double getPushRadius()
    {
    	return pushRadius;
    }
    
    //七牛上传秘钥
    public String getUpToken()
    {
    	return getProperty("UpToken");
    }
    
    public void setUpToken(String upToken)
    {
    	setProperty("UpToken", upToken);
    }

   
 

    /**
     * 清除app缓存
     */
/*    public void clearAppCache() {
        DataCleanManager.cleanDatabases(this);
        // 清除数据缓存
        DataCleanManager.cleanInternalCache(this);
        // 2.2版本才有将应用缓存转移到sd卡的功能
        if (isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)) {
            DataCleanManager.cleanCustomCache(MethodsCompat
                    .getExternalCacheDir(this));
        }
        // 清除编辑器保存的临时内容
        Properties props = getProperties();
        for (Object key : props.keySet()) {
            String _key = key.toString();
            if (_key.startsWith("temp"))
                removeProperty(_key);
        }
        new KJBitmap().cleanCache();
    }
 
*/
    /**
     * 判断当前版本是否兼容目标版本的方法
     * 
     * @param VersionCode
     * @return
     */
    public static boolean isMethodsCompat(int VersionCode) {
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        return currentVersion >= VersionCode;
    }

    public static boolean isFristStart() {
        return getPreferences().getBoolean(KEY_FRITST_START, true);
    }

    public static void setFristStart(boolean frist) {
        set(KEY_FRITST_START, frist);
    }
    
    
 
    
	/**
	 * 缓存时间
	 * @return
	 */
    protected static long getAutoRefreshTime() {
        return  45*60; //半小时
    } 
    
	/*private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 
	
	private static HttpClient getHttpClient(){
		HttpClient httpClient = new HttpClient(connectionManager);
		//设置默认的超市重试处理策略
		httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
		//参数定义每台主机允许的最大连接数
		 httpClient.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(3);
		//设置连接超时时间
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT_CONNECTION);
		//设置读取数据超时时间
		httpClient.getHttpConnectionManager().getParams().setSoTimeout(TIMEOUT_SOCKET);
		//设置主机地址
		httpClient.getHostConfiguration().setHost(URLs.API_HOST);
		//设置禁用cookie提高性能
		httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
		//设置字符集
		httpClient.getParams().setContentCharset(UTF_8);
		
		return httpClient;
	}*/
	
}
